import service from "@/utils/axios";

enum Api{
    Login="/admin/user/login",
    UserList="/user/page",
    UserInfo="/user/info",
    CheckUserNo="/user/check-userno",
}

interface LoginParams{
    username:string;
    password:string
}

interface UserListParams{
    pageNum: number;
    pageSize: number;
    userId?: string;
    userNo?: string;
    nickname?: string;
}

interface UserInfoParams{
    userId?: string;
    userNo?: string;
}

interface UserInfoData{
    nickname: string;
    birth: string;
    avatar: string;
    gender: number;
    coin: number;
    diamond: number;
    countryCode: string | null;
    divide: number;
}

interface UserInfoResponse{
    code: number;
    data: UserInfoData;
    timestamp: number;
}

// 用户基础信息
interface UserBaseInfo {
    userId?: string
    goodNo?: string
    userNo: string
    nickname: string
    avatar: string
    gender: number | null
    hasPrettyNo: boolean
    birth: string
    region?: string
    createTime?: number
    userStatus: number
    countryCode?: string
    appLanguage?: string
    userData?: any
    remark?: string
    background?: string[]
    roomId?: string
    relation?: any
    guildNo?: string
}

interface UserInfo{
    id: string;
    createUser: string | null;
    createTime: string;
    modifyUser: string | null;
    modifyTime: string;
    goodNo: string | null;
    userNo: string;
    nickname: string;
    avatar: string;
    gender: number | null;
    birth: string;
    email: string | null;
    phone: string | null;
    region: string | null;
    countryCode: string | null;
    appLanguage: string;
    appVersion: string;
    userRoleType: number;
    status: number;
    logoffStatus: number | null;
    deviceNo: string;
    lastLoginTime: string | null;
    lastLoginIp: string | null;
    lastLoginOs: string | null;
    remark: string | null;
    packageNo: string | null;
    updateTimes: number | null;
    background: string[] | null;
    divide: number;
}

interface UserListResponse{
    code: number;
    data: {
        hasNextPage: boolean;
        list: UserInfo[];
        nextPage: number;
        count: number;
        total: number;
        size: number;
    };
    timestamp: number;
}

function loginApi(data:LoginParams):Promise<any>{
    console.log('发送登录参数 (JSON):', {
        username: data.username,
        password: data.password
    });
    
    // 使用JSON传参
    return service.post(Api.Login, data, {
        headers: {
            'Content-Type': 'application/json'
        }
    })
}

function userListApi(data:UserListParams):Promise<UserListResponse>{
    return service.post(Api.UserList, data, {
        headers: {
            'Content-Type': 'application/json'
        }
    })
}

function getUserInfoApi(data:UserInfoParams):Promise<UserInfoResponse>{
    return service.post(Api.UserInfo, data, {
        headers: {
            'Content-Type': 'application/json'
        }
    })
}

// 校验用户编号是否存在
function checkUserNoApi(userNo: string): Promise<{ code: number; data: UserBaseInfo; message?: string }> {
    return service.get(Api.CheckUserNo, { params: { userNo } })
}

export {loginApi, userListApi, getUserInfoApi, checkUserNoApi}
export type {UserInfo, UserListParams, UserListResponse, UserInfoParams, UserInfoData, UserInfoResponse, UserBaseInfo}